package ink.onei.production.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.onei.production.entity.Customer;
import ink.onei.production.entity.Goods;
import ink.onei.production.entity.Order;
import ink.onei.production.exception.OperateException;
import ink.onei.production.service.ICustomerService;
import ink.onei.production.service.IGoodsService;
import ink.onei.production.service.IOrderService;
import ink.onei.production.vo.OrderDetailVO;
import ink.onei.production.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: nekotako
 * @Description: 计划进度控制层
 * @Date: 26/12/2023 20:48 Tuesday
 */

@RestController
@RequestMapping("/plan")
public class PlanController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IGoodsService goodsService;

    private ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping("/orderAdd")
    public Result<Order> orderAdd(@RequestBody OrderDetailVO orderDetail) {
        Result<Order> result = new Result<>();
        Integer i = null;
        try {
            i = orderService.add(orderDetail);
        } catch (OperateException e) {
            if (e.getMessage().equals("客户不存在"))
                result.setCode(401);
            else if (e.getMessage().equals("商品不存在"))
                result.setCode(402);

            result.setMsg(e.getMessage());
            return result;
        }
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/orderModify")
    public Result<Order> orderModify(@RequestBody Order order) {
        Integer i = orderService.modify(order);
        Result<Order> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/orderDelete")
    public Result<Order> orderDelete(@RequestBody List<Order> orderList) {
        //public Result<Order> orderDelete(@RequestBody Order order) {
        //Integer i = orderService.delete(order);
        Integer i = orderService.deleteBatch(orderList);
        Result<Order> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/orderDetail")
    public Result<OrderDetailVO> orderDetail(String page, String limit,
                                             @RequestParam(required = false) String id,
                                             @RequestParam(required = false) String customerName,
                                             @RequestParam(required = false) String goodsName,
                                             @RequestParam(required = false) String priceUnit,
                                             @RequestParam(required = false) String orderStatus) {
        Integer idInt = (id == null || id.isEmpty() ? null : Integer.parseInt(id));
        OrderDetailVO orderDetail = new OrderDetailVO();
        int i = Integer.parseInt(page);
        int j = Integer.parseInt(limit);
        PageHelper.startPage(i, j);
        List<OrderDetailVO> orderList = orderService.getOrderList(orderDetail, idInt, customerName, goodsName, priceUnit, orderStatus);
        PageInfo<OrderDetailVO> pageInfo = new PageInfo<>(orderList);
        Result<OrderDetailVO> result = new Result<>();
        result.setData(orderList);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @RequestMapping("/customerAdd")
    public Result<Customer> customerAdd(@RequestBody Customer customer) {
        Integer i = customerService.add(customer);
        Result<Customer> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/customerModify")
    public Result<Customer> customerModify(@RequestBody Customer customer) {
        Integer i = customerService.modify(customer);
        Result<Customer> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/customerDelete")
    public Result<Customer> customerDelete(@RequestBody List<Customer> customerList) {
        Integer i = customerService.deleteBatch(customerList);
        //public Result<Customer> customerDelete(@RequestBody Customer customer) {
        //Integer i = customerService.delete(customer);
        Result<Customer> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/customerDetail")
    public Result<Customer> customerDetail(String page, String limit,
                                           @RequestParam(required = false) String id,
                                           @RequestParam(required = false) String name,
                                           @RequestParam(required = false) String fullName,
                                           @RequestParam(required = false) String fax,
                                           @RequestParam(required = false) String mail,
                                           @RequestParam(required = false) String manager,
                                           @RequestParam(required = false) String address,
                                           @RequestParam(required = false) String phone,
                                           @RequestParam(required = false) String status) {
        Integer idInt = (id == null || id.isEmpty() ? null : Integer.parseInt(id));
        int i = Integer.parseInt(page);
        int j = Integer.parseInt(limit);
        PageHelper.startPage(i, j);
        List<Customer> customerList = customerService.getCustomerList(idInt, name, fullName, fax, mail, manager, address, phone, status);
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        Result<Customer> result = new Result<>();
        result.setData(customerList);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @RequestMapping("/goodsAdd")
    public Result<Goods> goodsAdd(@RequestBody Goods goods) {
        Integer i = goodsService.add(goods);
        Result<Goods> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/goodsModify")
    public Result<Goods> goodsModify(@RequestBody Goods goods) {
        Integer i = goodsService.modify(goods);
        Result<Goods> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/goodsDelete")
    public Result<Goods> goodsDelete(@RequestBody List<Goods> goodsList) {
        Integer i = goodsService.deleteBatch(goodsList);
        //public Result<Goods> goodsDelete(@RequestBody Goods goods) {
        //Integer i = goodsService.delete(goods);
        Result<Goods> result = new Result<>();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("/goodsDetail")
    public Result<Goods> goodsDetail(String page, String limit,
                                     @RequestParam(required = false) String id,
                                     @RequestParam(required = false) String name,
                                     @RequestParam(required = false) String type,
                                     @RequestParam(required = false) String status) {

        Integer idInt = (id == null || id.isEmpty() ? null : Integer.parseInt(id));
        int i = Integer.parseInt(page);
        int j = Integer.parseInt(limit);
        PageHelper.startPage(i, j);
        List<Goods> goodsList = goodsService.getGoodsList(idInt, name, type, status);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Result<Goods> result = new Result<>();
        result.setData(goodsList);
        result.setTotal(pageInfo.getTotal());
        return result;
    }
}
